package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.OrderWriteOffLog;
import com.tebiecloud.order.server.service.OrderWriteOffLogService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 订单核销记录 前端控制器
 * @author tebie
 * @date 2019-10-07
 */
@RestController
@RequestMapping("/orderwriteofflog")
public class OrderWriteOffLogController {

    @Autowired
    private OrderWriteOffLogService orderWriteOffLogService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<OrderWriteOffLog>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        OrderWriteOffLog query = pageParams.mapToObject(OrderWriteOffLog.class);
        QueryWrapper<OrderWriteOffLog> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderWriteOffLogService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<OrderWriteOffLog> get(@RequestParam("id") Long id) {
        OrderWriteOffLog entity = orderWriteOffLogService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param orderGoodsId 订单商品id,
     * @param storeId 门店id,
     * @param userId 核销人id,
     * @param userName 核销人名称,
     * @param price 核销的订单费用（单位：分）,
     * @param payType 支付类型：1微信支付，2现金支付，3刷卡支付,
     * @param receiver 收货人,
     * @param receiverHeadimgurl 收货人头像
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "orderGoodsId", required = false) Long orderGoodsId,
        @RequestParam(value = "storeId", required = false) Long storeId,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "userName", required = false) String userName,
        @RequestParam(value = "price", required = false) Integer price,
        @RequestParam(value = "payType", required = false) Integer payType,
        @RequestParam(value = "receiver", required = false) String receiver,
        @RequestParam(value = "receiverHeadimgurl", required = false) String receiverHeadimgurl
    ) {
        OrderWriteOffLog entity = new OrderWriteOffLog();
        entity.setOrderGoodsId(orderGoodsId);
        entity.setStoreId(storeId);
        entity.setUserId(userId);
        entity.setUserName(userName);
        entity.setPrice(price);
        entity.setPayType(payType);
        entity.setReceiver(receiver);
        entity.setReceiverHeadimgurl(receiverHeadimgurl);
        orderWriteOffLogService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param orderGoodsId 订单商品id
     * @param storeId 门店id
     * @param userId 核销人id
     * @param userName 核销人名称
     * @param price 核销的订单费用（单位：分）
     * @param payType 支付类型：1微信支付，2现金支付，3刷卡支付
     * @param receiver 收货人
     * @param receiverHeadimgurl 收货人头像
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "orderWriteOffId", required = false) Long orderWriteOffId,
            @RequestParam(value = "orderGoodsId", required = false) Long orderGoodsId,
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "payType", required = false) Integer payType,
            @RequestParam(value = "receiver", required = false) String receiver,
            @RequestParam(value = "receiverHeadimgurl", required = false) String receiverHeadimgurl
    ) {
        OrderWriteOffLog entity = new OrderWriteOffLog();
        entity.setOrderWriteOffId(orderWriteOffId);
        entity.setOrderGoodsId(orderGoodsId);
        entity.setStoreId(storeId);
        entity.setUserId(userId);
        entity.setUserName(userName);
        entity.setPrice(price);
        entity.setPayType(payType);
        entity.setReceiver(receiver);
        entity.setReceiverHeadimgurl(receiverHeadimgurl);
        orderWriteOffLogService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderWriteOffLogService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderWriteOffLogService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
